rm(list=ls())
gc()

## 06 - ESTIMATE TSMART PID MODELS


## LOAD PACKAGES
library(data.table)
library(lfe)
library(stringr)


data = fread('panel-analysis-2012-2016.csv.gz',select = c('voterbase_id_year1','voterbase_id_year2','DemDiff','RepDiff','DemDiff2','RepDiff2','Party_2016','Party_2020','DemSpExpDiff_nohh', 'RepSpExpDiff_nohh','DemSpExp_nohh_year1','RepSpExp_nohh_year1','Age_year1','Race','Gender',
                                                                            'ZipCode','Married_year1','countyfips','Party_year1','hh.n.adj_year1','hh.n.adj_year2',
                                                                            'hh.d.adj_year1','hh.d.adj_year2','hh.r.adj_year1','hh.r.adj_year2',
                                                                            'State',  'WhiteBlockGroupDiff','MarriageDiff','AgeBlockGroupDiff','RegsBlockGroupDiff',
                                                                           'HHIncomeBlockGroupDiff' , 'CollegeBlockGroupDiff' , 'HomeownerBlockGroupDiff' , 'YearBuiltBlockGroupDiff' , 
                                                                           'DriveWorkBlockGroupDiff' , 'EmplBlockGroupDiff' , 'HouseValueBlockGroupDiff',
             paste0(c('DemSpExp_nohh','RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year1'),
             paste0(c('DemSpExp_nohh', 'RepSpExp_nohh','hh.n.adj','hh.d.adj','hh.r.adj'), '_alt_year2')))

data[DemDiff==1 | Party_year1=='Democrat'&DemDiff==0,Party_2016:='Democrat']
data[RepDiff==1 | Party_year1=='Republican'&RepDiff==0,Party_2016:='Republican']
data[is.na(Party_2016), Party_2016:='Other']
data[Party_2020=='',Party_2020:=NA]

data[!Party_year1%in%c('Democrat','Republican'), Party_year1:='Other']
data[countyfips=='',countyfips:=NA]
data[ZipCode=='',ZipCode:=NA]
data[Gender=='',Gender:=NA]


data[,hh.n.diff:=hh.n.adj_alt_year2-hh.n.adj_alt_year1]
data[,hh.d.diff:=hh.d.adj_alt_year2-hh.d.adj_alt_year1]
data[,hh.r.diff:=hh.r.adj_alt_year2-hh.r.adj_alt_year1]

data[,DemSpExpDiff_nohh_alt := DemSpExp_nohh_alt_year2-DemSpExp_nohh_alt_year1]
data[,RepSpExpDiff_nohh_alt := RepSpExp_nohh_alt_year2-RepSpExp_nohh_alt_year1]


data = data[!is.na(DemSpExpDiff_nohh_alt)|!is.na(RepSpExpDiff_nohh_alt)]

data[,DemDiff2:=as.numeric(Party_2020=='Democrat')-as.numeric(Party_2016=='Democrat')]
data[,RepDiff2:=as.numeric(Party_2020=='Republican')-as.numeric(Party_2016=='Republican')]



# split data

dems = data[Party_year1=='Democrat' & Party_2016=='Democrat']
reps = data[Party_year1=='Republican' & Party_2016=='Republican']
oths = data[Party_year1=='Other' & Party_2016=='Other']



rm(data)
gc()

# construct grouping variables 
# we can drop rows where any of these are missing



dems = dems[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)  &!is.na(ZipCode)&!is.na(Married_year1)]
reps = reps[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race)& !is.na(ZipCode)&!is.na(Married_year1)]
oths = oths[!is.na(Age_year1) & !is.na(Gender) & !is.na(Race) &  !is.na(ZipCode)&!is.na(Married_year1)]

dems[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
reps[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
oths[,AgeDecile:=cut(Age_year1,breaks=unique(as.numeric(quantile(Age_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]


# spatial seg treatment
# democrat exposure
dems[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
reps[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
oths[,DemSpDecile:=cut(DemSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(DemSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]

# republican exposure
dems[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
reps[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]
oths[,RepSpDecile:=cut(RepSpExp_nohh_alt_year1,breaks=unique(as.numeric(quantile(RepSpExp_nohh_alt_year1, probs=seq(0,1,by=.1),na.rm=T))),include.lowest=T)]



#####
# make groups
# spatial seg treatment
dems[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]
reps[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]
oths[,GroupSpDemExp:=paste(ZipCode,AgeDecile,Race, Gender, DemSpDecile,State,hh.n.adj_alt_year1,hh.d.adj_alt_year1,Married_year1, sep ='_')]

dems[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]
reps[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]
oths[,GroupSpRepExp:=paste(ZipCode,AgeDecile,Race, Gender, RepSpDecile,State,hh.n.adj_alt_year1,hh.r.adj_alt_year1,Married_year1, sep ='_')]



# estimate models

# spatial seg treatment
ModelDemSpExpDems = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt + hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
ModelDemSpExpReps = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)
ModelDemSpExpOths = summary(felm(DemDiff2 ~ DemSpExpDiff_nohh_alt+ hh.d.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpDemExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpDemExp)&!is.na(countyfips)]), robust =T)

ModelRepSpExpDems = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = dems[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
ModelRepSpExpReps = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = reps[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)
ModelRepSpExpOths = summary(felm(RepDiff2 ~ RepSpExpDiff_nohh_alt+ hh.r.diff+hh.n.diff+WhiteBlockGroupDiff+MarriageDiff+AgeBlockGroupDiff+RegsBlockGroupDiff+
                                   HHIncomeBlockGroupDiff + CollegeBlockGroupDiff + HomeownerBlockGroupDiff + YearBuiltBlockGroupDiff + 
                                   DriveWorkBlockGroupDiff + EmplBlockGroupDiff + HouseValueBlockGroupDiff|GroupSpRepExp|0|countyfips, data = oths[!grepl('_NA_',GroupSpRepExp)&!is.na(countyfips)]), robust =T)



save(ModelDemSpExpDems, ModelDemSpExpReps, ModelDemSpExpOths, 
     ModelRepSpExpDems, ModelRepSpExpReps, ModelRepSpExpOths,
     
     
     file = paste0('results/future-results-2012-2016-alt.Rdata'))

